package com.zqxx.examine.portal.controller.page;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationServiceException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.model.news.News;
import com.zqxx.examine.model.user.Department;
import com.zqxx.examine.portal.security.UserInfo;
import com.zqxx.examine.portal.service.ExamService;
import com.zqxx.examine.portal.service.NewsService;
import com.zqxx.examine.portal.service.QuestionService;
import com.zqxx.examine.portal.service.UserService;

@Controller
public class UserPage
{
  @Autowired
  private UserService userService;
  @Autowired
  private ExamService examService;
  @Autowired
  private QuestionService questionService;
  @Autowired
  private NewsService newsService;
  
  @RequestMapping(value={"/user-login-page"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String loginPage(Model model, @RequestParam(value="result", required=false, defaultValue="") String result, HttpServletRequest request)
  {
    AuthenticationServiceException exception = (AuthenticationServiceException)request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
    if ((exception != null) && (
      ("准考证号码错误！".equals(exception.getMessage())) || ("不在考试时间范围内，不允许使用准考证！".equals(exception.getMessage())))) {
      return "redirect:quick-start";
    }
    return "login";
  }
  
  @RequestMapping(value={"/quick-start"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String quickStartPage(Model model, @RequestParam(value="result", required=false, defaultValue="") String result)
  {
    return "quick-start";
  }
  
  @RequestMapping(value={"/user-register"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String registerPage(Model model)
  {
    List<Department> depList = this.userService.getDepList(null);
    model.addAttribute("depList", depList);
    return "register";
  }
  
  @RequestMapping(value={"/"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String homePage(Model model)
  {
    return "redirect:home";
  }
  
  @RequestMapping(value={"home"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String directToBaseHomePage(Model model, HttpServletRequest request)
  {
    Page<News> pageModel = new Page();
    pageModel.setPageNo(1);
    pageModel.setPageSize(2);
    List<News> newsList = this.newsService.getNewsList(pageModel);
    model.addAttribute("newsList", newsList);
    
    request.getSession().removeAttribute("SPRING_SECURITY_LAST_EXCEPTION");
    String result = request.getParameter("result");
    if ("failed".equals(result)) {
      model.addAttribute("result_msg", "登陆失败");
    }
    if (SecurityContextHolder.getContext().getAuthentication() == null) {
      return "home";
    }
    if (SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString().endsWith("anonymousUser")) {
      return "home";
    }
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (userInfo.getHistId() != 0) {
      return "redirect:student/exam-start/" + userInfo.getExamId();
    }
    return "home";
  }
  
  @RequestMapping(value={"student/setting"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String settingPage(Model model)
  {
    List<Department> depList = this.userService.getDepList(null);
    model.addAttribute("depList", depList);
    return "setting";
  }
  
  @RequestMapping(value={"student/change-password"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String changePasswordPage()
  {
    return "change-password";
  }
  
  @RequestMapping(value={"news/{newsId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String newsDetailPage(Model model, @PathVariable("newsId") int newsId)
  {
    News news = this.newsService.getNewsById(newsId);
    model.addAttribute("news", news);
    return "news-detail";
  }
}
